<?php

    require_once getenv('DOCUMENT_ROOT')."/lib/config.inc";
    require_once "system.inc";
    require_once "biorhythm.inc";

    define('COL_WIDTH',    10);
    define('COL_BRD_WIDTH', 1);
    define('IMG_HEIGHT',  200);
    define('DAYS_BEFORE',   7);
    define('DAYS_AFTER',   30);
    define('DAYS_CALC', DAYS_BEFORE + 1 + DAYS_AFTER);
    define('IMG_HEIGHT_DIV_2', floor(( IMG_HEIGHT - 2 * COL_BRD_WIDTH ) / 2));
    define('IMG_WIDTH', COL_WIDTH * DAYS_CALC);
    
    if ( ! empty($_GET['birthday']) ) {
        
        $birthday = $_GET['birthday'];
        $today = mktime();
        $firstday = $today - DAYS_BEFORE * SEC_IN_DAY;
            
        $img = imageCreateTrueColor(IMG_WIDTH, IMG_HEIGHT);
        $alpha_bg = 0;
        $alpha_cl = 30;
        $alpha_negative = 95;   
         
        $cl_cell_now_border      = imageColorAllocateAlpha($img, 170,  96,  68, $alpha_bg);
        $cl_cell_now_background  = imageColorAllocateAlpha($img, 243, 199, 182, $alpha_bg);
        $cl_cell_holl_border     = imageColorAllocateAlpha($img, 124, 170,  68, $alpha_bg);
        $cl_cell_holl_background = imageColorAllocateAlpha($img, 255, 255, 200, $alpha_bg);
        $cl_cell_job_border      = imageColorAllocateAlpha($img, 118, 118, 118, $alpha_bg);
        $cl_cell_job_background  = imageColorAllocateAlpha($img, 182, 182, 182, $alpha_bg);
        
        $cl_cell_negative_background  = imageColorAllocateAlpha($img, 0, 0, 0, $alpha_negative);
        
        $cl_physical     = imageColorAllocateAlpha($img, 211, 91, 33, $alpha_cl);
        $cl_emotional    = imageColorAllocateAlpha($img, 91, 211, 33, $alpha_cl);
        $cl_intellectual = imageColorAllocateAlpha($img, 33, 91, 211, $alpha_cl);
        
        imagesetthickness($img, 5);
        
        for ( $count = 0; $count < DAYS_CALC; $count++ ) {
        	
            $calc_date = $firstday + SEC_IN_DAY * $count;
            $physical     = biorhythm::physical($birthday, $calc_date);
            $emotional    = biorhythm::emotional($birthday, $calc_date);
            $intellectual = biorhythm::intellectual($birthday, $calc_date);
                
            if ( $calc_date == $today ) {
                $cl_bg = $cl_cell_now_background;
                $cl_br = $cl_cell_now_border;
            } elseif ( date('w', $calc_date) == 0 || date('w', $calc_date) == 6 ) {
                $cl_bg = $cl_cell_holl_background;
                $cl_br = $cl_cell_holl_border;
            } else {
                $cl_bg = $cl_cell_job_background;
                $cl_br = $cl_cell_job_border;
            }	
            
            $xpos     = COL_WIDTH * $count;
            $xpos_end = $xpos + COL_WIDTH - 1;
            
            $ypos_ph = ( $physical     + 1 ) * IMG_HEIGHT_DIV_2 + COL_BRD_WIDTH;
            $ypos_em = ( $emotional    + 1 ) * IMG_HEIGHT_DIV_2 + COL_BRD_WIDTH;
            $ypos_in = ( $intellectual + 1 ) * IMG_HEIGHT_DIV_2 + COL_BRD_WIDTH;
            
            imageFilledRectangle($img, $xpos, 0, $xpos_end, IMG_HEIGHT - 1, $cl_br);
            imageFilledRectangle($img, $xpos + COL_BRD_WIDTH, COL_BRD_WIDTH, $xpos_end - COL_BRD_WIDTH, IMG_HEIGHT - COL_BRD_WIDTH -1, $cl_bg);
            imageFilledRectangle($img, $xpos, IMG_HEIGHT_DIV_2, $xpos_end, IMG_HEIGHT - 1, $cl_cell_negative_background);
            /*
            imageFilledRectangle($img, $xpos + COL_BRD_WIDTH, $ypos_ph, $xpos_end - COL_BRD_WIDTH, IMG_HEIGHT - COL_BRD_WIDTH -1, $cl_physical);
            imageFilledRectangle($img, $xpos + COL_BRD_WIDTH, $ypos_em, $xpos_end - COL_BRD_WIDTH, IMG_HEIGHT - COL_BRD_WIDTH -1, $cl_emotional);
            imageFilledRectangle($img, $xpos + COL_BRD_WIDTH, $ypos_in, $xpos_end - COL_BRD_WIDTH, IMG_HEIGHT - COL_BRD_WIDTH -1, $cl_intellectual);
            */
			/*
            imageFilledRectangle($img, $xpos + COL_BRD_WIDTH, $ypos_ph, $xpos_end - COL_BRD_WIDTH, $ypos_ph + 5, $cl_physical);
            imageFilledRectangle($img, $xpos + COL_BRD_WIDTH, $ypos_em, $xpos_end - COL_BRD_WIDTH, $ypos_em + 5, $cl_emotional);
            imageFilledRectangle($img, $xpos + COL_BRD_WIDTH, $ypos_in, $xpos_end - COL_BRD_WIDTH, $ypos_in + 5, $cl_intellectual);
            */
            
            $y_ph = $ypos_ph;
            $y_em = $ypos_em;
            $y_in = $ypos_in;
            $x = floor($xpos + COL_WIDTH / 2); 
            if ( $count ) {
                imageline($img, $x_old, $y_ph_old, $x, $y_ph, $cl_physical);
                imageline($img, $x_old, $y_em_old, $x, $y_em, $cl_emotional);
                imageline($img, $x_old, $y_in_old, $x, $y_in, $cl_intellectual);
            }
            $y_ph_old = $y_ph;
            $y_em_old = $y_em;
            $y_in_old = $y_in;
            $x_old    = $x;
        }
        
        header('Content-type: image/png');
        imagepng($img);
    }
?>